
/*var isSubmit = 0;// 0 可以提交，1 不可提交
function editPic(imgSort, isadd) {
    sessionStorage.setItem("isadd",isadd);
    sessionStorage.setItem("imgSort",imgSort);
    layer_show("修改图片","integralSkuPic.html","1400","800","100","200");
}
function addPic(imgSort, isadd) {
    sessionStorage.setItem("isadd",isadd);
    sessionStorage.setItem("imgSort",imgSort);
    layer_show("添加图片","integralSkuPic.html","1400","800","100","200");
}*/
//图片上传
function addBtn(val) {

  $("#fileUp"+val).trigger('click');

}
function ueCallBack(data) {
    if (!data){
        return ;
    }
    for(var i = 0; i < data.data.length; i++){
        var value = "<img src='" + data.data[i].imgUrl + "'>";
        UE.getEditor('editor').execCommand('insertHtml', value)
    }
}
function forJsFileBtn(e,val) {
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            $.Huimodalalert("请选择图片类型", 2000);
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function (e) {
            var es= $("#forJsFileBtn")[0].files[0]
            getupfilerul(es,val,ueCallBack)
        }
    }
}
function putDataToPage(e,val) {
    //console.log($("#intro").val())
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            $.Huimodalalert("请选择图片类型", 2000);
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function (e) {
               var es= $("#fileUp"+val)[0].files[0]
                getupfilerul(es,val)
        }
    }

}
function getupfilerul(can,val,callBack){
    console.log(can)
    if(!verificationPicFileSize(can)){
        $.Huimodalalert('图片大小限制不能超过1M',2000);
        return ;
    }
    var formData = new FormData();
    var that = this;
    formData.append('file',can);
    $.ajax({
        url: root.goodsInter.goods_uploadIntegralSkuImgs,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        data: formData,
        success: function (data) {
            if (callBack){
                callBack.call(that,data) ;
            }else{
                $("#myImg"+val).attr("src", data.data[0].imgUrl);
                $("#myImg"+val).attr("name", data.data[0].imgFileName);
                $("#myImg"+val).attr("onclick",'picShow('+val+')')
                //编辑和删除展示
                $("#addBtn"+val).removeAttr("style")
                $("#addBtn"+val).attr("style","color: #1476e5")
                $("#deleteimg"+val).removeAttr("style")
                $("#deleteimg"+val).attr("style","color: #1476e5")

            }
        }
    })
}
//图片大小验证
function verificationPicFileSize(file) {
    var flag=false;
    var fileSize = 0;
    var fileMaxSize = Number(1024);
    if(file){
        fileSize =file.size;
        var size = fileSize / Number(1024);
        if (size > fileMaxSize) {
            flag=false;
        }else{
            flag=true;
        }
    }
    return flag;
}
//预览图片
function picShow(val) {
    var imgUrl= $("#myImg"+val).attr("src")
    var title="查看图片"
    var url="../IntegralGoods/picShow.html"
    window.event ? window.event.cancelBubble = true : e.stopPropagation();
    sessionStorage.setItem("imgUrl", imgUrl);
    layer_show(title, url, "800", "500");
}
//删除图片
function deleteimg(val) {
    $("#myImg"+val).attr("src", "../../../../img/tianjia.png");
    $("#myImg"+val).attr("onclick",'addBtn('+val+')');
    $("#addBtn"+val).removeAttr("style")
    $("#addBtn"+val).attr("style","color: floralwhite")
    $("#deleteimg"+val).removeAttr("style")
    $("#deleteimg"+val).attr("style","color: floralwhite")
}



//批量上传
var upFileList = [] ;
function bulkaddBtn() {
    if($("#myImg1").attr('src')=="../../../../img/tianjia.png"){
        $.Huimodalalert("请选择主图", 2000);
        return false;
    }
    var numss=0
    var coners=5
    for(var h=1; h<=coners; h++){
        if($("#myImg"+h).attr('src')=="../../../../img/tianjia.png"){
            numss++;
        }}
    if(numss==0){
        $.Huimodalalert("图片暂时不可使用批量上传", 2000);
        return false;
    }
    upFileList = []
    var fileUpEl = document.getElementById('fileUp') ;
    fileUpEl.outerHTML=fileUpEl.outerHTML;
    $("#fileUp").trigger('click');
}
function bulkUpload(e,can) {
    verificationPicFileType6(can)
    verificationPicFileSize6(can)
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }
        upFileList.push(file) ;
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function(e) {

        }
    }
    bulkUpload2(can);
}
function bulkUpload2(can) {
    var formData = new FormData();
    var that = this;
    for (var i=0;i<upFileList.length;i++){
        formData.append('file',upFileList[i]);
    }
    $.ajax({
        url: root.goodsInter.goods_uploadIntegralSkuImgs,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        data: formData,
        success: function (data) {
            var num=-1
            var nums=0
            var coner=5
            for(var k=1; k<=coner; k++){
                if($("#myImg"+k).attr('src')=="../../../../img/tianjia.png"){
                    nums++;
                }}
            if(data.data!=""){
                for(var i=1; i<=coner; i++){
                    if($("#myImg1").attr('src')!="../../../../img/tianjia.png"){
                        if($("#myImg"+i).attr('src')=="../../../../img/tianjia.png"){
                            num++;
                            if(data.data.length>nums){
                                $.Huimodalalert('上传数量超上限',2000);
                                return false;
                            }else {
                                $.Huimodalalert('上传成功',2000);
                            }
                            var imgUrl=data.data[num].imgUrl
                           /* var imgUrls=imgUrl.lastIndexOf("\?");
                            imgUrl=imgUrl.substring(0,imgUrls);*/
                            $("#myImg" +i).attr('src', imgUrl)
                            $("#myImg" +i).attr("name", data.data[num].imgFileName)
                            $("#myImg" +i).attr("onclick", 'picShow(' + i + ')')

                            $("#addBtn" + i).removeAttr("style")
                            $("#addBtn" + i).attr("style", "color: #1476e5")
                            $("#deleteimg" + i).removeAttr("style")
                            $("#deleteimg" + i).attr("style", "color: #1476e5")
                        }
                    }
                }
            }else {
                $.Huimodalalert('图片格式或大小不符合上传规则',2000);

            }

        }
    })
}
// 图片类型验证
function verificationPicFileType6(file) {
    var flag=false;
    var fileTypes = [".jpg", ".png",".jpeg"];
    var filePath = file.value;
    if(filePath){
        var isNext = false;
        var fileEnd = filePath.substring(filePath.indexOf("."));
        for (var i = 0; i < fileTypes.length; i++) {
            if (fileTypes[i] == fileEnd) {
                isNext = true;
                flag= true;
                break;
            }
        }
        if (!isNext){
            file.value = "";
            flag=false;
        }
    }
    return flag;
}
//图片大小验证
function verificationPicFileSize6(file) {
    var flag=false;
    var fileSize = 0;
    var fileMaxSize = Number(1024);
    var filePath = file.value;
    if(filePath){
        fileSize =file.files[0].size;
        var size = fileSize / Number(1024);
        if (size > fileMaxSize) {
            file.value = "";
            flag=false;
        }else{
            flag=true;
        }
    }
    return flag;
}

//验证顺序
function pictures(val,nums) {
    if(Number($(val).val())>5){
        $.Huimodalalert("最多5张图片", 2000);
        $(".pictures"+nums).val("")
        return false;
    }

}








//获取用户信息
var localStorageParameters = decodeURIComponent(web.readCookie("loginData"));
var JsonObject =JSON.parse(localStorageParameters);

function checkUnitValue(unitValue) {
    if (unitValue.length>6){
        $.Huimodalalert("计量单位最大长度为6个字符", 2000);
        $("#unit").focus() ;
    }
    return false ;
}
var isPass = true;
function makeSure() {
    isPass = true;
    var integralSkuName = $("#integralSkuName").val()?$("#integralSkuName").val().replace(/\s/g, ""):null ;
    if(integralSkuName == null || integralSkuName == '' ){
        $.Huimodalalert("请输入积分商品名称", 2000);
        return false;
    }
    var unit = $("#unit").val()?$("#unit").val().replace(/\s/g, ""):null ;
    if(unit == null || unit == '' ){
        $.Huimodalalert("请填写计量单位", 2000);
        return false;
    }else {
        $("#remark").css({
            border: "1px #ddd solid"
        });
        $(".remarkErrorMsg").css({
            display:"none"
        });

        var imgs = new Array();
        var integralSkuId = $("#integralSkuId").html() ;

        for(var i=1;i<6;i++){
            var img = {} ;
            if($(".pictures"+i).val()==""){
                $.Huimodalalert("图片顺序不能为空", 3000);
                return false;
            }
            if($(".pictures"+i).val()==0){
                $.Huimodalalert("图片顺序不能为0", 3000);
                return false;
            }
            img.integralSkuId = integralSkuId ;
            img.imgType = 1==i?1:0 ;
            img.imgUrl = $("#myImg"+i).attr('src') ;
            img.imgSort = $(".pictures"+i).val() ;
            img.imgName = $("#myImg"+i).attr('name') ;
            img.integralSkuCode = $("#integralSkuCode").html() ;
            if (img.imgUrl && !(img.imgUrl === '../../../../img/tianjia.png')){
                imgs.push(img) ;
            }else if (1==i){
                $.Huimodalalert("请上传主图", 3000);
                return false;
            }

        }
        //图片顺序不可重复
        $.each(imgs,function(i,first){
            $.each(imgs,function(s,sed){
                if (i==s){
                    return true;
                }
                var fiestname=first.imgSort+first.imgSort
                var sedname=sed.imgSort+sed.imgSort
                if(fiestname!=""||sedname!=""){
                    if (fiestname==sedname){
                        if(first.imgSort!=""&&first.imgSort!=""){
                                $.Huimodalalert("图片顺序不可重复", 2000);
                                 isPass = false;
                                return false;
                        }
                    }

                }

            })
        })
        if (!isPass) {
            return false;
        }

        $.ajax({
            contentType:'application/json',
            url: root.goodsInter.goods_updateIntegralSku,
            cache: false,  //禁用缓存
            type: "POST",
            data: JSON.stringify({
                "detail": UE.getEditor('editor').getContent(),
                "unitName": $("#unit").val(),
                "integralSkuId": integralSkuId,
                "integralSkuCode": $("#integralSkuCode").html(),
                "integralSkuName": $("#integralSkuName").val(),
                "remark": $("#remark").val(),
                "integralSkuImgs": imgs,//图片
                "lastModifiedBy":JsonObject.user.name
            }),
            success: function (data) {
                if(data.errorCode == 0){
                    layer.msg("保存成功",{icon: 6,time:1000},function () {
                        var index =  parent.location.reload();
                        parent.layer.close(index);
                    });
                }else {
                    $.Huimodalalert(data.msg, 3000);
                }
            }
        });
    }
}